<template>
  <div>
    <router-view v-slot="{ Component }">
      <transition name="fade" mode="out-in" appear>
          <component :is="Component" />
      </transition>
    </router-view>

      <van-tabbar route v-model="active" v-show="sy_hidden">
        <van-tabbar-item to="/home" icon="home-o">首页</van-tabbar-item>
        <van-tabbar-item to="/category" icon="gift-o">礼物</van-tabbar-item>
        <van-tabbar-item to="/cart" icon="cart-o">车车</van-tabbar-item>
        
        <van-tabbar-item to="/user" icon="setting-o" v-if="message > 0" :badge="message">我的</van-tabbar-item>
        <van-tabbar-item to="/user" icon="setting-o" v-else>我的</van-tabbar-item>

      </van-tabbar>


  </div>
</template>

<script>
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
import { reactive, toRefs, computed } from 'vue'
import { ref } from 'vue';
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'

  export default {
    setup() {
      const router = useRouter()
      const route = useRoute()

      const store = useStore()

      router.beforeEach((to, from) => {
        let sy_hidden_range = ["goods", "detail", "cart", "login"] // 隐藏 底部导航栏的页面

          console.log(store.state.syHidden);
          console.log(to)
          console.log(from)
          console.log(route)
        if ( to.name == from.name ) {
          router.back()
        }

        if(sy_hidden_range.some(v => to.name.startsWith(v))) 
        {
          store.commit("falseHidden");
        } else {
          store.commit("trueHidden");
        }
        
        // if (to.meta.index > from.meta.index) {
        //   state.transitionName = 'slide-left' // 向左滑动
        // } else if (to.meta.index < from.meta.index) {
        //   // 由次级到主级
        //   state.transitionName = 'slide-right'
        // } else {
        //   state.transitionName = ''   // 同级无过渡效果
        // }
      })
      
      const message = computed(() => {
        return store.state.message
      })

      const sy_hidden = computed(() => {
        return store.state.syHidden
      })

      const active = ref(0);
      
      return { active, message, sy_hidden };
    }
  }
</script>

<style lang="less">
html, body {
  height: 100%;
  overflow-x: hidden;
  overflow-y: scroll;
}
#app {
  height: 100%;
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  // text-align: center;
  color: #2c3e50;
}

.router-view{
    width: 100%;
    height: auto;
    position: absolute;
    top: 0;
    bottom: 0;
    margin: 0 auto;
    -webkit-overflow-scrolling: touch;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active{
    height: 100%;
    will-change: transform;
    transition: all 500ms;
    position: absolute;
    backface-visibility: hidden;
}
.slide-right-enter{
    opacity: 0;
    transform: translate3d(-100%, 0, 0);
}
.slide-right-leave-active{
    opacity: 0;
    transform: translate3d(100%, 0, 0);
}
.slide-left-enter{
    opacity: 0;
    transform: translate3d(100%, 0, 0);
}
.slide-left-leave-active{
    opacity: 0;
    transform: translate3d(-100%, 0, 0);
}

.van-badge--fixed {
  z-index: 1000;
}

.van-action-bar {
  z-index: 1000;
}

</style>
